package com.spring.gop.proj.service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.gop.proj.interfaces.EventDao;
import com.spring.gop.proj.interfaces.EventService;
import com.spring.gop.proj.model.Event;

@Service
public class EventServiceImpl implements EventService {
	
	@Autowired EventDao eventDao;

	public List<Event> selectEvents() {
		String startDate;
		String endDate;
		
		Calendar cal = Calendar.getInstance();
		startDate = Integer.toString(cal.get(cal.YEAR));
		if(cal.getTime().getMonth() + 1 < 10) startDate += "-0" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-01";
		else startDate += "-" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-01";
		
		endDate = Integer.toString(cal.get(cal.YEAR));
		if(cal.getTime().getMonth() + 1 < 10) endDate += "-0" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-" + Integer.toString(cal.getActualMaximum(cal.DAY_OF_MONTH));
		else endDate += "-" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-" + Integer.toString(cal.getActualMaximum(cal.DAY_OF_MONTH));
		
		return eventDao.selectEvents(startDate, endDate);
	}
	
	public List<Event> selectLastEvent() {
		String startDate;
		String endDate;
		
		Calendar cal = Calendar.getInstance();
		startDate = Integer.toString(cal.get(cal.YEAR));
		if(cal.getTime().getMonth() + 1 < 10) startDate += "-0" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-01";
		else startDate += "-" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-01";
		
		endDate = Integer.toString(cal.get(cal.YEAR));
		if(cal.getTime().getMonth() + 1 < 10) endDate += "-0" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-" + Integer.toString(cal.getActualMaximum(cal.DAY_OF_MONTH));
		else endDate += "-" + Integer.toString(cal.get(cal.getTime().getMonth() + 1)) + "-" + Integer.toString(cal.getActualMaximum(cal.DAY_OF_MONTH));
		
		return eventDao.selectLastEvent(startDate, endDate);
	}
	
	public Event selectEvent(int event_num) {
		return eventDao.selectEvent(event_num);
	}
}



























